#!/usr/bin/perl -w
########################################################################
# script name:  info_users.pl
# Used for:
# Author:       Martin Dumont
# Contributors:
# Version:      0.1
#
# (date format: date +'%Y/%m/%d')
#
# Date          Version Contributors    Modifications
# ----------    ------- ------------    -------------
# 2011/02/25    0.1     MDUMONT         initial
########################################################################
use strict;
my $type="user";

my $hostname = `/bin/uname -n`; chomp $hostname;
my $hostid = `/usr/bin/hostid`; chomp $hostid;
my $dt = `/bin/date +"%Y-%m-%d %H:%M:%S"`; chomp $dt;

my $outfile = "/var/OPM/data/${hostname}_${type}.out";
open (OUT, ">$outfile");
print OUT '<uname -n>',"\n",$hostname,"\n",'</uname -n>', "\n";
print OUT '<hostid>',"\n",$hostid,"\n",'</hostid>', "\n";
print OUT '<date>',"\n",$dt,"\n",'</date>', "\n";

my(%UIDS, %GIDS, %GROUP,%SHADOW, %USERS, %SUDO);

my(%PROJECT,%CRON,$cron);

if (-e "/etc/cron.d/cron.allow")
{ 
	$cron = "RESTRICTED"; 
	my @TMP = `/bin/cat /etc/cron.d/cron.allow`;
	foreach my $tmp (@TMP){chomp $tmp;$CRON{$tmp}++;}
} 
else {$cron = "N/A";}

# Processing "sudoers"
{
	my %ALIASES;
	my (@TMP);
	if (-e "/etc/sudoers"){@TMP=`cat /etc/sudoers`;}
	if (-e "/usr/local/etc/sudoers"){@TMP=`cat /usr/local/etc/sudoers`;}
	foreach my $tmp (@TMP)
	{
		$tmp =~ s/\#.*//g; $tmp =~ s/\s+/ /g; $tmp =~ s/^ //g; $tmp =~ s/ $//g;
		if ($tmp eq ""){}
		elsif ($tmp =~ /^Cmnd_Alias [A-Z0-9_-]+ *= */){}
		elsif ($tmp =~ /^Host_Alias [A-Z0-9_-]+ *= */){}
		elsif ($tmp =~ /^Runas_Alias [A-Z0-9_-]+ *= */){}
		elsif ($tmp =~ /^User_Alias ([A-Z0-9_-]+) *= *(.+)/){
			my ($alias, $users) = ($1,$2);
			my @TMP2=split /\s*,\s*/, $users;
			$ALIASES{$alias}=\@TMP2;
			
		}
		elsif ($tmp =~ /^([\w\d_-]+) ([\w\d_-]+) *= *(.+)/)
		{
			my ($u,$w,$c) = ($1,$2,$3);
			if (exists $ALIASES{$u})
			{
				foreach my $u2 (@{$ALIASES{$u}})
				{
					if (exists ( $SUDO{$u2})) { push @{$SUDO{$u2}}, $c; } 
					else { $SUDO{$u2} = [$c]; } 
				}
			}
			else {
				if (exists ( $SUDO{$u})){ push @{$SUDO{$u}}, $c;} 
				else {$SUDO{$u} = [$c];} 
			}
			
		}
		else {
		}
	
	}
}
	
# Processing "shadow"
{
	my (@TMP) = `cat /etc/shadow`;
	foreach my $tmp (@TMP){
		chop($tmp);
		my ($user, $pass,$chg, $daysmin,$daysmax,$dayswarn,$daysinact,$expire,$flag) = split /:/, $tmp;
		if ($pass eq "bxJV6tWKU8Ckg"){$pass = "NC";}
		if ($pass =~ m/^\*/) { $pass = "LK";}
		if (exists($SHADOW{$user})){
			print STDERR "Error 1 entry repeated in shadow for $user\n";
		}
		else {
			$SHADOW{$user} = [$pass,$chg, $daysmin,$daysmax,$dayswarn,$daysinact,$expire,$flag];
		}
	}
}

# Processing "group"
{
	print OUT '<group>',"\n";
	my (@TMP) = `cat /etc/group`;;
	foreach my $tmp (@TMP){
		my ($grp,$x,$gid,$users) = split /:/, $tmp;
		$users =~ s/\s+//;
		$GIDS{$gid}=$grp;
		print OUT "$grp\t$gid\t$users\n";
		my @TMP2 = split /,/, $users;
		foreach my $user (@TMP2){
			if (exists ( $GROUP{$user})){ push @{$GROUP{$user}}, $grp;} 
			else {$GROUP{$user} = [$grp];} 
		}
	}
	print OUT '</group>',"\n";
}
	

# Processing "passwd"
{
	print OUT '<users>',"\n";
	my (@TMP) = `cat /etc/passwd`;;
	foreach my $tmp (@TMP){
		$tmp =~ s/\t/ /g; $tmp =~ s/  / /g; chomp $tmp;
		my ($user,$x,$uid,$gid,$full,$home,$shell) = split /:/, $tmp;
		my $grp="" ; if (exists($GIDS{$gid})){$grp = $GIDS{$gid};}
		my $groups="" ; if (exists($GROUP{$user})){$groups = join ",",@{$GROUP{$user}};}
		###########################################################################
		# Gives:
		# mdumont  pts/0        216.113.24.30    Fri Feb 25 06:56   still logged in
		#
		# wtmp begins Wed Feb  2 05:00:28 2011
		###########################################################################
		# my $tmp2 = `/usr/bin/last -n 1 $user|/bin/grep $user`;
		# $tmp2 =~ s/\t/ /g;
		# my @TMP2 = split /\s+/,$tmp2;
		# shift @TMP2;shift @TMP2;shift @TMP2; pop @TMP2;
		# my $lastlogin = ($tmp2=~/^$user/)?join(" ",@TMP2):"-";
		###########################################################################
		my ($pass,$chg, $daysmin,$daysmax,$dayswarn,$daysinact,$expire,$flag)=("-","-","-","-","-","-","-","-");
		if (exists($SHADOW{$user})){($pass,$chg, $daysmin,$daysmax,$dayswarn,$daysinact,$expire,$flag) = @{$SHADOW{$user}};}
		else {print STDERR "Error No shadow entry for $user\n";}
		my $cron_allowed = $cron; if (exists($CRON{$user})){$cron_allowed = "ALLOWED";}
		print OUT "$uid\t$user\t$gid\t$grp\t$groups\t$full\t$home\t$shell\t";
		print OUT "$pass\t$chg\t$daysmin\t$daysmax\t$dayswarn\t$daysinact\t$expire\t$cron_allowed\t$flag\n";
	}
	print OUT '</users>',"\n";
	
}
	
close OUT;
`chown saunix $outfile`;
	
sub HomeSize {
	my $home = shift;
	my $size = -1;
	if (-e $home && $home ne "/"){
		my $du = `/usr/bin/du -ks $home`;
		chomp $du;
		if ($du =~ /.+\s(\d+)\s*$/){$size = $1;}
	}
	return $size;
}
